<template>
  <div>
    <!--Layout布局-->
    <el-row>
      <el-col :span="24">
        <el-row :gutter="20">
          <el-col :span="6">
            <!--搜索区域-->
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.keyword"
              clearable
              @clear="getCompoundList"
            >
              <el-button slot="append" icon="el-icon-search" @click="getCompoundList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="2.5">
            <el-button type="primary" @click="addDialog">添加化合物</el-button>
          </el-col>
          <el-col :span="2.5">
            <el-button type="danger" @click="batchDeleteUser">批量删除</el-button>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="24">
        <!--表格-->
        <el-table :data="compoundList" stripe @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="comnameCh" label="化合物中文名称"></el-table-column>
          <el-table-column prop="english" label="英文名称"></el-table-column>
          <el-table-column prop="formula" label="分子式"></el-table-column>
          <el-table-column prop="monoisotopicMass" label="单一同位素质量"></el-table-column>
          <el-table-column prop="skeletonType" label="Skeleton Type"></el-table-column>
          <el-table-column label="操作">
            <!-- 作用域插槽 -->
            <template slot-scope="scope">
              <!--修改按钮-->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-zoom-in"
                @click="showSeeDialog(scope.row)"
              ></el-button>
              <!--删除按钮-->
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="removeUserById(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-size="queryInfo.pageSize"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-row>

    <!--修改用户的对话框-->
    <el-dialog
      :title="seeUp ? '新增' : '修改'"
      :visible.sync="seeDialog"
      :before-close="handleClose"
      width="70%"
    >
      <!--内容主体区域-->
      <el-form :model="seeDialogData" label-width="150px">
        <el-form-item label="化合物名称" prop="comnameCh">
          <el-input v-model="seeDialogData.comnameCh"></el-input>
        </el-form-item>
        <el-form-item label="英文名称" prop="english">
          <el-input v-model="seeDialogData.english"></el-input>
        </el-form-item>
        <el-form-item label="分子式" prop="formula">
          <el-input v-model="seeDialogData.formula"></el-input>
        </el-form-item>

        <el-form-item label="单一同位素质量" prop="monoisotopicMass">
          <el-input v-model="seeDialogData.monoisotopicMass"></el-input>
        </el-form-item>

        <el-form-item label="Skeleton Type" prop="skeletonType">
          <el-input v-model="seeDialogData.skeletonType"></el-input>
        </el-form-item>

        <el-form-item label="结构式" prop="construction_img">
          <el-upload
            class="avatar-uploader"
            action="/api/compound/upload"
            name="uploadFile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="seeDialogData.constructionImg"
              :src="seeDialogData.constructionImg"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="CAS" prop="cas">
          <el-input v-model="seeDialogData.cas"></el-input>
        </el-form-item>

        <el-form-item label="IUPAC Name" prop="iupacName">
          <el-input v-model="seeDialogData.iupacName"></el-input>
        </el-form-item>

        <el-form-item label="Chemspider" prop="chemspider">
          <el-input v-model="seeDialogData.chemspider"></el-input>
        </el-form-item>
        <el-form-item label="HMDB" prop="hmdb">
          <el-input v-model="seeDialogData.hmdb"></el-input>
        </el-form-item>
        <el-form-item label="Pubchem" prop="pubchem">
          <el-input v-model="seeDialogData.pubchem"></el-input>
        </el-form-item>
        <el-form-item label="KEGG" prop="kegg">
          <el-input v-model="seeDialogData.kegg"></el-input>
        </el-form-item>
        <el-form-item label="毒性Toxicological Information(LD50)" prop="toxicologicalInf">
          <el-input v-model="seeDialogData.toxicologicalInf"></el-input>
        </el-form-item>
        <el-form-item label="临床中毒症状" prop="toxicSymptom">
          <el-input v-model="seeDialogData.toxicSymptom"></el-input>
        </el-form-item>
        <el-form-item label="Literature(Poisoning syptoms)" prop="literaturePoisonSym">
          <el-input v-model="seeDialogData.literaturePoisonSym"></el-input>
        </el-form-item>
        <el-form-item label="中毒机制" prop="intoxicationMechanism">
          <el-input v-model="seeDialogData.intoxicationMechanism"></el-input>
        </el-form-item>
        <el-form-item label="中毒机制文献" prop="literatureIntoxicMecH">
          <el-input v-model="seeDialogData.literatureIntoxicMecH"></el-input>
        </el-form-item>
        <el-form-item label="[M+H]+" prop="mh">
          <el-input v-model="seeDialogData.mh"></el-input>
        </el-form-item>
        <el-form-item label="[M+Na]+" prop="mna">
          <el-input v-model="seeDialogData.mna"></el-input>
        </el-form-item>
        <el-form-item label="[M+K]+" prop="mk">
          <el-input v-model="seeDialogData.mk"></el-input>
        </el-form-item>
        <el-form-item label="[M+NH4]+" prop="mnh4">
          <el-input v-model="seeDialogData.mnh4"></el-input>
        </el-form-item>
        <el-form-item label="[2M+H]+" prop="m2H">
          <el-input v-model="seeDialogData.m2H"></el-input>
        </el-form-item>
        <el-form-item label="[M-H]-" prop="mhload">
          <el-input v-model="seeDialogData.mhload"></el-input>
        </el-form-item>
        <el-form-item label="[M+Cl]-" prop="mcl">
          <el-input v-model="seeDialogData.mcl"></el-input>
        </el-form-item>
        <el-form-item label="MS/MS spectrum(m/z, %relative intensity)" prop="msSpectrum ">
          <el-input v-model="seeDialogData.msSpectrum"></el-input>
        </el-form-item>
        <el-form-item label="MS conditions" prop="msConditions">
          <el-input v-model="seeDialogData.msConditions"></el-input>
        </el-form-item>
      </el-form>
      <!--底部按钮区域-->
      <span slot="footer" class="dialog-footer" v-if="seeUp == false">
        <el-button @click="seeDialog = false">取 消</el-button>
        <el-button type="primary" @click="editUser">修改</el-button>
      </span>
      <span slot="footer" class="dialog-footer" v-else>
        <el-button @click="seeDialog = false">取 消</el-button>
        <el-button type="primary" @click="addUser">新增</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { compoundList, compoundAdd, compoundUpdate, compoundDelete, compoundBatchDelete } from "@/api/compound";
export default {
  data() {
    return {
      compoundList: [], // 用户列表
      total: 0, // 用户总数
      // 获取用户列表的参数对象
      queryInfo: {
        keyword: "", // 查询参数
        pageNo: 1, // 当前页码
        pageSize: 5, // 每页显示条数
      },
      seeUp: false,
      seeDialog: false, // 控制添加用户对话框是否显示
      seeDialogData: {},
      dialogImageUrl: '',
      dialogVisible: false,
      multipleSelection: [],
      ids: [],
    };
  },
  created() {
    // 生命周期函数
    this.getCompoundList();
  },
  methods: {
    getCompoundList() {
      compoundList(this.queryInfo)
        .then((res) => {
          if (res.data.code === 200) {
            //用户列表
            this.compoundList = res.data.data.records;
            this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageSize = newSize;
      // 重新发起请求用户列表
      this.getCompoundList();
    },
    // 监听 当前页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pageNo = newPage;
      // 重新发起请求用户列表
      this.getCompoundList();
    },
    //添加用户
    addUser() {
      compoundAdd(this.seeDialogData)
        .then((res) => {
          if (res.data.code === 200) {
            this.seeDialog = false;
            this.getCompoundList();
            this.$message({
              message: "添加成功",
              type: "success",
            });
          } else {
            this.$message.error("添加失败");
          }
        })
        .catch((err) => {
          this.$message.error("添加异常");
          console.log(err);
        });
    },
    //上传成功后赋值
    handleAvatarSuccess(res) {
      this.seeDialogData.constructionImg = res.data;
    },

    addDialog() {
      this.seeDialogData = {};
      this.seeDialog = true;
      this.seeUp = true;
    },

    // 监听 修改
    showSeeDialog(userinfo) {
      this.seeDialog = true;
      this.seeDialogData = userinfo;
    },
    handleClose(done) {
      this.seeUp = false;
    },

    //修改用户
    editUser() {
      compoundUpdate(this.seeDialogData)
        .then((res) => {
          if (res.data.code === 200) {
            this.seeDialog = false;
            this.getCompoundList();
            this.$message({
              message: "修改成功",
              type: "success",
            });
          } else {
            this.$message.error("修改失败");
          }
        })
        .catch((err) => {
          this.$message.error("修改异常");
          console.loge(err);
        });
    },
    // 根据ID删除对应的用户信息
    async removeUserById(id) {
      // 弹框 询问用户是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult == "confirm") {
        //删除用户
        compoundDelete(id)
          .then((res) => {
            if (res.data.code === 200) {
              this.getCompoundList();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            } else {
              this.$message.error("删除失败");
            }
          })
          .catch((err) => {
            this.$message.error("删除异常");
            console.log(err);
          });
      }
    },
    //批量选中事件处理
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
      //向被删除的ids赋值
      this.multipleSelection.forEach((item) => {
        this.ids.push(item.id);
        console.log(this.ids);
      });
    },
    //批量删除用户
    async batchDeleteUser() {
      // 弹框 询问用户是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消删除，则返回值为字符串 cancel
      if (confirmResult == "confirm") {
        //批量删除用户
        compoundBatchDelete(this.ids)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message({
                message: "批量删除成功",
                type: "success",
              });
              this.getCompoundList();
            } else {
              this.$message.error("批量删除失败");
            }
          })
          .catch((err) => {
            this.$message.error("批量删除异常");
            console.log(err);
          });
      }
    }
  },
};
</script>

<style>
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
}
.el-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1) !important;
  height: 60pt;
}
</style>
